﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace LeapInterface
{
    class Keyboard
    {
        [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetKeyboardState(byte[] lpKeyState);

        const uint KEYEVENTF_KEYUP = 0x0002;
        const uint KEYEVENTF_EXTENDEDKEY = 0x0001;

        public static SByte shift = (SByte)16;
        public static SByte ctrl = (SByte)17;
        public static SByte alt = (SByte)18;

        /*
        public static int KeyPress(SByte VK_KEY)
        {
            if (MyGlobals.allowKeyboard || !MyGlobals.debug)
                keybd_event((byte)VK_KEY, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
            else
                Console.WriteLine("press "+VK_KEY);
            return 0; 
        }

        public static int KeyRelease(SByte VK_KEY)
        {
            if (MyGlobals.allowKeyboard || !MyGlobals.debug)
                keybd_event((byte)VK_KEY, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
            else
                Console.WriteLine("release " + VK_KEY);
            return 0;
        }
        */
        public static int KeyPress(int id)
        {
            
            if (MyGlobals.allowKeyboard || !MyGlobals.debug)
            {
                if (MyGlobals.ctrl[id])
                    keybd_event((byte)ctrl, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
                else if (MyGlobals.alt[id])
                    keybd_event((byte)alt, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
                else if (MyGlobals.shift[id])
                    keybd_event((byte)shift, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);

                keybd_event((byte)MyGlobals.tabKey[id], 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
            }
            else
            {
                if (MyGlobals.ctrl[id])
                    Console.WriteLine("press " + ctrl);
                else if (MyGlobals.alt[id])
                    Console.WriteLine("press " + alt);
                if (MyGlobals.shift[id])
                    Console.WriteLine("press " + shift); 

                Console.WriteLine("press " + MyGlobals.tabKey[id]);
            }
            return 0;
        }

        public static int KeyRelease(int id)
        {
            if (MyGlobals.allowKeyboard || !MyGlobals.debug)
            {
                if (MyGlobals.ctrl[id])
                    keybd_event((byte)ctrl, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
                else if (MyGlobals.alt[id])
                    keybd_event((byte)alt, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
                else if (MyGlobals.shift[id])
                    keybd_event((byte)shift, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);

                keybd_event((byte)MyGlobals.tabKey[id], 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
            }
            else
            {
                if (MyGlobals.ctrl[id])
                    Console.WriteLine("release " + ctrl);
                else if (MyGlobals.alt[id])
                    Console.WriteLine("release " + alt);
                else if (MyGlobals.shift[id])
                    Console.WriteLine("release " + shift);

                Console.WriteLine("release " + MyGlobals.tabKey[id]);
            }
            return 0;
        }
    }
}
